using FactoryMethod.Interface;
using FactoryMethod.Service;
using System;
using System.Configuration;
using System.Reflection;

namespace FactoryMethod
{
    /// <summary>
    /// 工厂类，通过入参判断需要创建的具体类型
    /// </summary>
    public class ObjectFactory
    {
        public static IRace CreateInstance(RaceType raceType)
        {
            IRace race = raceType switch
            {
                RaceType.Human => new Human(),
                RaceType.NE => new NE(),
                RaceType.ORC => new ORC(),
                RaceType.Undead => new Undead(),
                RaceType.Fifth => new FifthRace(),
                _ => throw new Exception("wrong raceType"),
            };
            return race;
        }

        public static string IRaceType = ConfigurationManager.AppSettings["IRaceType"];
        public static IRace CreateInstanceConfig()
        {
            RaceType raceType = (RaceType)Enum.Parse(typeof(RaceType), IRaceType);
            return CreateInstance(raceType);
        }

        public static string IRaceTypeReflection = ConfigurationManager.AppSettings["IRaceTypeReflection"];
        public static IRace CreateInstanceConfigReflection()
        {
            Assembly assembly = Assembly.Load(IRaceTypeReflection.Split(',')[1]);
            Type type = assembly.GetType(IRaceTypeReflection.Split(',')[2]);
            return (IRace)Activator.CreateInstance(type);
        }
    }

    public enum RaceType
    {
        Human,
        NE,
        ORC,
        Undead,
        Fifth
    }
}
